<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "{{%playlog}}".
 *
 * @property string $logId
 * @property integer $createdAt
 * @property integer $isAward
 * @property string $openid
 *
 * @property User $user
 */
class Playlog extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%playlog}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['createdAt', 'isAward', 'openid'], 'required'],
            [['createdAt', 'isAward'], 'integer'],
            [['openid'], 'string', 'max' => 40],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'logId' => 'Log ID',
            'createdAt' => 'Created At',
            'isAward' => 'Is Award',
            'openid' => 'Openid',
        ];
    }

    public function beforeValidate()
    {
        if ($this->isNewRecord) {
            $this->createdAt = time();
            $this->openid = Yii::$app->user->id;
        }
        return parent::beforeValidate();
    }


    public function getUser()
    {
        return $this->hasOne(User::className(), ['openid' => 'openid']);
    }
}
